package com.qidian.k12.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//@Component
//@Aspect
class MyAspects {
    private List<Long> times = new ArrayList<>();

    //切点
    @Pointcut("execution(* com.qidian.k12.service.TemplateService.queryById(..))")
    public void templateQueryById() {
    }

    @Around("templateQueryById()")
    public Object timeTest(ProceedingJoinPoint joinPoint) {
        Object retVal = null;
        long start = System.currentTimeMillis();

        try {

            retVal = joinPoint.proceed();

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        long end = System.currentTimeMillis();
        long time = end - start;
        //System.out.println(times.size() + "--" + time);
        times.add(time);
        synchronized (this) {
            if (times.size() > 5000) {
                Long max = Collections.max(times);
                System.out.println("5000次测试的访问耗时为：" + max);
                times.clear();
            }
        }


        return retVal;
    }
}
